package com.example.springbott.service;

import com.example.springbott.bean.MarketAdmin;
import com.example.springbott.bean.MarketAdminExample;
import com.example.springbott.bean.common.BasePageInfo;
import com.example.springbott.bean.common.CommonData;
import com.example.springbott.mapper.MarketAdminMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;


@Service
public class AdminAdminServiceImpl implements AdminAdminService {
    @Autowired
    MarketAdminMapper marketAdminMapper;
    //--------------------------小朱-------------------------

    /**
     * @author: 小朱
     * @Description: 查询功能
     * @date: 21:40 2022/11/20
     */
    @Override
    public CommonData adminAdminList(BasePageInfo basePageInfo, String username) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        //根据addtime降序,sql语句拼接
        marketAdminExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        if (username != null) {
            criteria.andUsernameLike("%" + username + "%");
        }
        criteria.andDeletedEqualTo(false);
        //根据username这个条件查询basePageInfo这个对象，list集合是因为可能查询出来的不止一个
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(marketAdminExample);
        for (int i = 0; i < marketAdmins.size(); i++) {
            marketAdmins.get(i).setPassword(null);
        }
        //分页功能
        PageInfo<MarketAdmin> marketAdminPageInfo = new PageInfo<>(marketAdmins);
        return CommonData.data(marketAdminPageInfo);
    }

    /**
     * @author: 小朱
     * @Description: 新增功能
     * @date: 21:58 2022/11/20
     */
    @Override
    @Transactional
    public MarketAdmin adminAdminCreate(MarketAdmin marketAdmin, MultipartFile file) throws IOException {
        marketAdmin.setUsername(marketAdmin.getUsername());
        marketAdmin.setPassword(marketAdmin.getPassword());
        marketAdmin.setRoleIds(marketAdmin.getRoleIds());
        marketAdmin.setAddTime(new Date());
        marketAdmin.setDeleted(false);
        marketAdmin.setUpdateTime(new Date());
        //marketAdmin.getAvatar(marketAdmin.getAvatar());
        marketAdminMapper.insertSelective(marketAdmin);
        return marketAdmin;
    }

    /**
     * @author: 小朱
     * @Description: 更新功能
     * @date: 15:18 2022/11/21
     */
    @Override
    @Transactional
    public MarketAdmin adminAdminUpdate(MarketAdmin marketAdmin) {
        marketAdmin.setId(marketAdmin.getId());
        marketAdmin.setUsername(marketAdmin.getUsername());
        marketAdmin.setPassword(marketAdmin.getPassword());
        marketAdmin.setAvatar(marketAdmin.getAvatar());
        marketAdmin.setRoleIds(marketAdmin.getRoleIds());
        marketAdminMapper.updateByPrimaryKey(marketAdmin);
        return marketAdmin;
    }

    /**
     * @author: 小朱
     * @Description: 删除功能(逻辑删除，数据库里面的数据不变，只是delete列由0（false）变为1（true）)
     * @date: 15:23 2022/11/21
     */
    @Override
    @Transactional
    public void adminAdminDelete(MarketAdmin marketAdmin) {
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        if (marketAdmin.getId()!=null) {
            criteria.andIdEqualTo(marketAdmin.getId());
        }
        marketAdmin.setDeleted(true);
        marketAdminMapper.updateByExampleSelective(marketAdmin, marketAdminExample);
    }
}
